package com.web.controller;

import com.web.bean.学生情况;
import com.web.dao.学生情况Dao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

//定义当前请求处理的访问uri
@WebServlet("/insertStudent")
public class StudentInsertController extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        String 学号 = req.getParameter("学号");
        String 姓名 = req.getParameter("姓名");
        String 性别 = req.getParameter("性别");
        String 出生日期 = req.getParameter("出生日期");
        String 生源地 = req.getParameter("生源地");
        String 院系代号 = req.getParameter("院系代号");
        String 政治面貌 = req.getParameter("政治面貌");
        String 宿舍电话 = req.getParameter("宿舍电话");

        学生情况 stu = new 学生情况(学号, 姓名, 性别, 出生日期, 生源地, 院系代号, 政治面貌, 1,宿舍电话,"1");
        学生情况Dao dao = new 学生情况Dao();
        boolean isok = false;
        try {
            isok = dao.insertStu(stu);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        if (isok) {
            req.getRequestDispatcher("/getAllStudent").forward(req, resp);
        } else {

            req.setAttribute("message", "添加操作失败，请重新操作");
            req.setAttribute("stu", stu);//保存对象数据，在页面表单中回显
            req.getRequestDispatcher("/StudentInsert.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}